package com.job.project.utils;

import lombok.*;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应信息主体
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class RestResponse<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int code;

	private String msg;

	private T data;

	public static <T> RestResponse<T> ok() {
		return build(null, HttpStatus.OK.value(), "success");
	}

	public static <T> RestResponse<T> ok(T data) {
		return build(data, HttpStatus.OK.value(), "success");
	}

	public static <T> RestResponse<T> ok(T data, String msg) {
		return build(data,HttpStatus.OK.value(), msg);
	}

	public static <T> RestResponse<T> ok(T data, int code, String msg) {
		return build(data,code, msg);
	}

	public static <T> RestResponse<T> failed() {
		return build(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), "failure");
	}

	public static <T> RestResponse<T> failed(String msg) {
		return build(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	public static <T> RestResponse<T> failed(int code,String msg) {
		return build(null, code, msg);
	}

	public static <T> RestResponse<T> failed(T data) {
		return build(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), "failure");
	}

	public static <T> RestResponse<T> failed(T data, String msg) {
		return build(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	public static <T> RestResponse<T> failed(T data, int code,String msg) {
		return build(data, code, msg);
	}


	public static <T> RestResponse<T> build(T data, int code, String msg) {
		RestResponse<T> apiResult = new RestResponse<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}


}
